package template

import (
	"cdam/models"
	"os"
	"strings"
	"text/template"
)

type ParseJava struct {

}

type YKJavaCode struct {
	TableName string
	TableComment string
	ClassName string
	PackageName string
	PathName string
	LowerClassName string
	Fields   []YKJavaTableField
}

type YKJavaTableField struct {
	Message string
	Length string
	Type string
	FieldName string
	LowerFieldName string
	Comment string
	NullAble string
	DataBaseType string
	TableFieldComment string
}

func (j *ParseJava) init() *ParseJava {
	return j
}

func (j *ParseJava) ParseToDbType(javaType string) string {
	if javaType == "String" {
		return "varchar"
	}else if javaType == "int" {
		return "int"
	}else{
		return javaType
	}

}

func (j *ParseJava) ConvertTo(data models.JavaCode)  YKJavaCode {
	var result = YKJavaCode{}
	result.TableName = data.TableName
	result.ClassName = data.ClassName
	result.TableComment = data.TableComment
	result.PackageName = data.PackageName
	result.LowerClassName = strings.ToLower(data.ClassName)
	result.PathName = strings.Replace(data.PackageName,".","/",-1)
	fields := []YKJavaTableField{}
	for _,value := range data.Fields  {
		y := YKJavaTableField{}
		y.Type =value.Type
		y.FieldName = value.FieldName
		y.LowerFieldName = strings.ToLower(value.FieldName)
		y.Length = value.Length
		y.DataBaseType = j.ParseToDbType(value.Type)
		y.Message = value.Message
		y.Comment =value.Comment
		y.NullAble = value.NullAble
		y.TableFieldComment = value.TableFieldComment
		fields = append(fields,y)
	}
	result.Fields = fields
	return result
}

func (j *ParseJava) ParseInitCode(data models.JavaCode) ([]string,error) {
	resultData := j.ConvertTo(data)
	// 创建model
	_dir := "./code/java/" + resultData.PathName + "/model"
	r,_ := PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	modelFileName := _dir + "/" + resultData.ClassName + ".java"
	file, err := os.OpenFile(modelFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err := template.ParseFiles("template/files/java/model.java.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result := []string{}
	result = append(result, modelFileName)
	// 创建repository
	_dir = "./code/java/" + resultData.PathName + "/repository"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	repositoryFileName := _dir + "/" + resultData.ClassName + "Repository.java"
	file, err = os.OpenFile(repositoryFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/java/repository.java.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, repositoryFileName)
	// 创建dto
	_dir = "./code/java/" + resultData.PathName + "/dto"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	dtoFileName := _dir + "/" + resultData.ClassName + "DTO.java"
	file, err = os.OpenFile(dtoFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/java/dto.java.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, dtoFileName)

	// 创建service接口

	_dir = "./code/java/" + resultData.PathName + "/service"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	serviceFileName := _dir + "/" + resultData.ClassName + "Service.java"
	file, err = os.OpenFile(serviceFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/java/service.java.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, serviceFileName)

	// 创建service实现

	_dir = "./code/java/" + resultData.PathName + "/service/impl"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	serviceImplFileName := _dir + "/" + resultData.ClassName + "ServiceImpl.java"
	file, err = os.OpenFile(serviceImplFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/java/serviceimpl.java.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, serviceImplFileName)

	// 创建controller

	_dir = "./code/java/" + resultData.PathName + "/controller"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	controllerImplFileName := _dir + "/" + resultData.ClassName + "Controller.java"
	file, err = os.OpenFile(controllerImplFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/java/controller.java.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, controllerImplFileName)

	// 创建convert


	_dir = "./code/java/" + resultData.PathName + "/convert"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	convertFileName := _dir + "/" + resultData.ClassName + "Convert.java"
	file, err = os.OpenFile(convertFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/java/convert.java.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, convertFileName)

	// 创建vue
	// index.js
	_dir = "./code/js/vue/" + resultData.LowerClassName
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	indexFileName := _dir + "/index.js"
	file, err = os.OpenFile(indexFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/neusoft/index.js.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, indexFileName)

	// tables.vue
	_dir = "./code/js/vue/" + resultData.LowerClassName
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	tableFileName := _dir + "/table.vue"
	file, err = os.OpenFile(tableFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/neusoft/table.vue.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, tableFileName)

	// actionBar.vue

	_dir = "./code/js/vue/" + resultData.LowerClassName + "/components"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	actionBarFileName := _dir + "/actionBar.vue"
	file, err = os.OpenFile(actionBarFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/neusoft/actionBar.vue.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, actionBarFileName)
	// editForm.vue

	_dir = "./code/js/vue/" + resultData.LowerClassName +"/components"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	editFormFileName := _dir + "/editForm.vue"
	file, err = os.OpenFile(editFormFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/neusoft/editForm.vue.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, editFormFileName)
	// tableFilter.vue

	_dir = "./code/js/vue/" + resultData.LowerClassName + "/components"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	tableFilterFileName := _dir + "/tablFilter.vue"
	file, err = os.OpenFile(tableFilterFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/neusoft/tableFilter.vue.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, tableFilterFileName)
	// viewForm.vue

	_dir = "./code/js/vue/" + resultData.LowerClassName + "/components"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	viewFormFileName := _dir + "/viewForm.vue"
	file, err = os.OpenFile(viewFormFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/neusoft/viewForm.vue.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, viewFormFileName)

	_dir = "./code/js/api"
	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	apiFileName := _dir + "/" + resultData.LowerClassName + ".js"
	file, err = os.OpenFile(apiFileName, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		return nil,err
	}
	tmpl, err = template.ParseFiles("template/files/neusoft/api.js.tpl")
	if err != nil {
		return  nil,err
	}
	err = tmpl.Execute(file, resultData)
	if err != nil {
		return nil,err
	}
	result = append(result, apiFileName)


	return result,err
}

func InitParseJavaEngine()  *ParseJava {
	e := new(ParseJava).init()
	return e
}
